package com.chen.qmzy.service.util;



import com.chen.qmzy.tool.FormwordTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**'
 * 推送消息 邮件短信钉钉微信
 */
@Component
public class PushMessageService {
    @Resource
    JavaMailSender javaMailSender;//注入java邮件 发送邮件
    @Resource
    private RedisTemplate<String,String> redisTemplate;//注入Redis
    @Resource
    FormwordTool formwordTool;//注入模板操作类
    private static final Logger logger= LoggerFactory.getLogger(PushMessageService.class);//日志getLogger需要传入的类名
    @Value("${spring.mail.username}")//邮箱发送地址
    private String fromMail;
    @Value("信息学院用户学生")//项目名称
    private String projectName;

    /**
     * 发送注册邮件
     * @param mailAddress 邮件地址
     * @return boolean true 发送成功 false 发送失败
     */
    public Boolean pushRegistMail(String mailAddress){
        Boolean flag=true;
        try {
            String randCode=String.valueOf(new Random().nextInt(10000,999999));
            redisTemplate.opsForValue().set("mail_Register_RandomCode_"+mailAddress,randCode,900, TimeUnit.SECONDS);//将邮箱地址和随机数存入Redis
            Map<String,String> map= new HashMap<>();
            map.put("[mailCode]",randCode);//传入替换的值，第一个参数是模板中的值，第二个参数需要修改的值
            map.put("[projectName]",projectName);
            String mailFormword  = formwordTool.setFiledList(map,"registmail");//第一个参数需要传入修改的参数，第二个参数传入模板的名称（不包含.html）
            SimpleMailMessage simpleMailMessage =new SimpleMailMessage(); //新建邮件对象
            simpleMailMessage.setFrom(fromMail);//发送邮箱地址
            simpleMailMessage.setTo(mailAddress);//收件人邮件地址
            simpleMailMessage.setSubject(projectName+"注册验证码");//设置邮件标题
            simpleMailMessage.setText(mailFormword);//设置发送内容
            javaMailSender.send(simpleMailMessage);
        }catch (Exception e){
            flag=false;
            logger.error(e.getMessage());
        }
        return true;
    }

    /**
     * 重新发送邮件
     * @param mailAddress 邮箱地址
     * @param text 内容
     * @return boolean
     */
    public Boolean rePushMail(String mailAddress,String text){
        Boolean flag=true;
        try {
            SimpleMailMessage simpleMailMessage =new SimpleMailMessage(); //新建邮件对象
            simpleMailMessage.setFrom(fromMail);//发送邮箱地址
            simpleMailMessage.setTo(mailAddress);//收件人邮件地址
            simpleMailMessage.setSubject(projectName+"--系统重发！");//设置邮件标题
            simpleMailMessage.setText(text);//设置发送内容
            javaMailSender.send(simpleMailMessage);
        }catch (Exception e){
            flag=false;
            logger.error(e.getMessage());
        }
        return flag;
    }
}
